<!doctype html>

<html>

  <head>
    <meta charset="utf-8">
    <title>WebAssembly Globals example</title>
  </head>
  <body>
  <div id="output"></div>
  <script>
    const output = document.getElementById("output");

    function assertEq(msg, got, expected) {
      output.innerHTML += `Testing ${msg}: `;
      if (got !== expected) {
        output.innerHTML += `FAIL!<br>Got: ${got}<br>Expected: ${expected}<br>`;
      } else {
        output.innerHTML += `SUCCESS! Got: ${got}<br>`;
      }
    }

    assertEq("WebAssembly.Global exists", typeof WebAssembly.Global, "function");

    const global = new WebAssembly.Global({ value: "i32", mutable: true }, 0);
    WebAssembly.instantiateStreaming(fetch("global.wasm"), { js: { global } })
    .then(({ instance }) => {
      assertEq("getting initial value from wasm", instance.exports.getGlobal(), 0);
      global.value = 42;
      assertEq("getting JS-updated value from wasm", instance.exports.getGlobal(), 42);
      instance.exports.incGlobal();
      assertEq("getting wasm-updated value from JS", global.value, 43);
    });
  </script>
  </body>

</html>
